package com.sync.syncmasterdata.dingrobot.service;


import com.aliyun.dingtalkoauth2_1_0.Client;
import com.aliyun.dingtalkoauth2_1_0.models.GetAccessTokenRequest;
import com.aliyun.dingtalkoauth2_1_0.models.GetAccessTokenResponse;
import com.aliyun.teaopenapi.models.Config;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sync.syncmasterdata.configuration.ConfInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 通用服务类
 */
@Service
public class DingUtilService {

    @Autowired
    private ConfInfo confInfo;

    @Autowired
    private ObjectMapper objectMapper;

    /**
     * 钉钉用服务
     * @return
     * @throws Exception
     */
    public String getTokendel() throws Exception {
        Config config = new Config();
        config.protocol = "https";
        config.regionId = "central";
        Client client = new Client(config);
        GetAccessTokenRequest getAccessTokenRequest = new GetAccessTokenRequest()
                // 机器人的appkey
                .setAppKey(this.confInfo.getRebotKey())
                // 机器人的appSecret
                .setAppSecret(this.confInfo.getRebotSecret());
        GetAccessTokenResponse rsp = client.getAccessToken(getAccessTokenRequest);
        System.out.println(objectMapper.writeValueAsString(rsp.getBody()));
        String access_token=rsp.getBody().getAccessToken();
        return access_token;
    }

}
